package cn.wjdiankong.levideo.hotsoon.data;

import org.json.JSONArray;
import org.json.JSONObject;

import android.text.TextUtils;
import android.util.Log;
import cn.wjdiankong.levideo.data.LevideoData;
import cn.wjdiankong.levideo.utils.Logger;
import cn.wjdiankong.levideo.utils.Utils;

public class HotsoonVideoData extends LevideoData{
	
	public static HotsoonVideoData fromJSONData(String jsonStr){
		HotsoonVideoData data = new HotsoonVideoData();
		if(TextUtils.isEmpty(jsonStr)){
			return data;
		}
		try{
			JSONObject json = new JSONObject(jsonStr);
			//video标签中的信息
			/**
			 * data
			 * 	-video
			 * 		-url_list
			 *  	-download_url
			 *  		-url_list
			 *  	-cover
			 *  		-url_list
			 *  	-width
			 *  	-height
			 *  
			 * 	-author
			 * 		-avatar_jpg
			 * 			-url_list
			 * 		-nickname
			 * 		-city
			 * 		-birthday_description
			 * 
			 * 	-stats
			 * 		-play_count
			 * 
			 * 	-title
			 * 
			 * 	-create_time
			 */
			
			//视频内容信息
			JSONObject dataJson = json.getJSONObject("data");
			JSONObject videoJson = dataJson.getJSONObject("video");
			JSONArray jsonAry = videoJson.optJSONArray("url_list");
			if(jsonAry != null && jsonAry.length() > 0){
				data.videoPlayUrl = jsonAry.getString(0);
			}
			jsonAry = videoJson.optJSONArray("download_url");
			if(jsonAry != null && jsonAry.length() > 0){
				data.videoDownloadUrl = jsonAry.getString(0);
			}
			
			JSONObject coverJson = videoJson.optJSONObject("cover");
			if(coverJson != null){
				jsonAry = coverJson.optJSONArray("url_list");
				if(jsonAry != null && jsonAry.length() > 0){
					data.coverImgUrl = jsonAry.getString(0);
				}
			}
			data.videoWidth = videoJson.optInt("width");
			data.videoHeight = videoJson.optInt("height");
			data.videoDuration = videoJson.optDouble("duration")+"";
			data.title = dataJson.optString("title");
			data.createTime = dataJson.optLong("create_time")*1000;
			
			JSONObject statJson = dataJson.optJSONObject("stats");
			if(statJson != null){
				data.playCount = statJson.optInt("play_count");
				data.likeCount = statJson.optInt("digg_count");
			}
			
			//视频作者信息
			JSONObject authorJson = dataJson.optJSONObject("author");
			if(authorJson != null){
				JSONObject thumbJson = authorJson.optJSONObject("avatar_jpg");
				if(thumbJson != null){
					jsonAry = thumbJson.optJSONArray("url_list");
					if(jsonAry != null && jsonAry.length() > 0){
						data.authorImgUrl = jsonAry.getString(0);
					}
				}
				data.authorName = authorJson.optString("nickname");
				data.authorSignature = authorJson.optString("signature");
				data.authorCity = authorJson.optString("city");
				data.authorAge = authorJson.optString("birthday_description");
				data.authorSex = authorJson.optInt("gender");
			}
			
			data.formatTimeStr = Utils.formatTimeStr(data.createTime);
			data.formatPlayCountStr = Utils.formatNumber(data.playCount);
			data.formatLikeCountStr = Utils.formatNumber(data.likeCount);
			if(data.title.length() > 30){
				data.filterTitleStr = Utils.filterStrBlank(data.title.substring(0, 30)+"...");
			}else{
				data.filterTitleStr = Utils.filterStrBlank(data.title);
			}
			if(data.authorName.length() > 7){
				data.filterUserNameStr = Utils.filterStrBlank(data.authorName.substring(0, 7)+"...");
			}else{
				data.filterUserNameStr = Utils.filterStrBlank(data.authorName);
			}
			
		}catch(Exception e){
			Logger.log("parse hotsoon json err:"+Log.getStackTraceString(e));
		}
		if(TextUtils.isEmpty(data.videoPlayUrl)){
			return null;
		}
		return data;
	}
	
	@Override
	public String toString(){
		return "videotitle="+title+",videoplayurl="+videoPlayUrl+",videodownloadurl="+videoDownloadUrl+",width="+videoWidth+",height="+videoHeight
				+",coverimgurl="+coverImgUrl+",authorname="+authorName+",authorimgurl="+authorImgUrl+",authorcity="+authorCity+",authorage="+authorAge
				+",videoplaycount="+playCount+",videoduration="+videoDuration;
	}
	
}
